package com.hb.trade.server.center.au.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hb.core.pojo.center.au.dto.OrgCollectionDTO;
import com.hb.core.pojo.center.au.entity.OrgCollection;
import com.hb.core.pojo.center.au.mapper.OrgCollectionMapper;
import com.hb.core.pojo.enums.PayTypeEnum;
import com.hb.trade.server.center.au.service.OrgCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fzzn.core.util.Enums;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 企业收款方式表 服务实现类
 * </p>
 *
 * @author zhongdl
 * @since 2022-01-15
 */
@Service
public class OrgCollectionServiceImpl extends ServiceImpl<OrgCollectionMapper, OrgCollection> implements OrgCollectionService {
    /**
     * @param orgId:
     * @Description: 查询企业的收款码列表信息
     * @Author: zhongdl
     * @Date:2022/1/17 9:55
     * @return: {@link List< OrgCollection> }
     **/
    @Override
    public List<OrgCollection> listOrgCollection(Long orgId) {
        if (orgId == null) {
            return CollUtil.list(false);
        }
        QueryWrapper<OrgCollection> wrapper = OrgCollection.wrapper();
        wrapper.eq("org_id", orgId);
        return list(wrapper);
    }

    /**
     * @param payeeId:
     * @Description: 根据收款方表id(逻辑删除)
     * @Author: zhongdl
     * @Date:2022/2/14 9:39
     * @return: {@link boolean }
     **/
    @Override
    public boolean removeByPayeeId(Long payeeId) {
        if (payeeId == null) {
            return true;
        }
        UpdateWrapper<OrgCollection> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("delete_flag", 1).eq("payee_id", payeeId);
        return update(updateWrapper);
    }

    /**
     * @param payeeId:
     * @Description: 根据收款方表id(物理删除)
     * @Author: zhongdl
     * @Date:2022/2/14 9:39
     * @return: {@link boolean }
     **/
    @Override
    public boolean deleteByPayeeId(Long payeeId) {
        if (payeeId == null) {
            return true;
        }
        QueryWrapper<OrgCollection> wrapper = OrgCollection.wrapper();
        wrapper.eq("payee_id", payeeId);
        return remove(wrapper);
    }

    @Override
    public boolean saveOrgCollection(List<OrgCollectionDTO> collectionDTOList) {
        if (CollUtil.isEmpty(collectionDTOList)) {
            return false;
        }
        List<OrgCollection> orgCollections = CollUtil.list(false);
        for (OrgCollectionDTO orgCollectionDTO : collectionDTOList) {
            OrgCollection orgCollection = orgCollectionDTO.to(OrgCollection.class);
            if (orgCollection.getId() == null) {
                orgCollection.buildBaseData(1);
            }
            orgCollections.add(orgCollection);
        }
        return saveOrUpdateBatch(orgCollections);
    }

    /**
     * @param payeeId: 收款码表id
     * @Description:
     * @Author: zhongdl
     * @Date:2022/2/14 10:45
     * @return: {@link List< OrgCollectionDTO> }
     **/
    @Override
    public List<OrgCollectionDTO> listOrgCollectionByPayeeId(Long payeeId) {
        return listOrgCollectionByPayeeId(payeeId, null);
    }

    @Override
    public List<OrgCollectionDTO> listOrgCollectionByPayeeId(Long payeeId, Integer payType) {
        List<OrgCollectionDTO> list = CollUtil.list(false);

        QueryWrapper<OrgCollection> wrapper = OrgCollection.wrapper("id", "ext_name", "file_name",
                "pay_type", "payee_id", "raw_file_name", "bank_account", "bank_account_name", "bank_deposit", "collection_oss");
        wrapper.eq("payee_id", payeeId).eq("delete_flag",0).eq(payType != null && payType > 0, "pay_type", payType);
        List<OrgCollection> orgCollections = list(wrapper);
        if (CollUtil.isEmpty(orgCollections)) {
            return list;
        }

        for (OrgCollection orgCollection : orgCollections) {
            PayTypeEnum payTypeEnum = Enums.of(PayTypeEnum.class, orgCollection.getPayType());

            // 信息不完整的不予返回
            OrgCollectionDTO collectionDTO = orgCollection.to(OrgCollectionDTO.class);
            switch (payTypeEnum) {
                case ALI_PAY:
                case WE_CHAT_PAY:
                    if (StrUtil.isEmpty(collectionDTO.getCollectionOss())) {
                        continue;
                    }
                    list.add(collectionDTO);
                    break;
                case UNION_PAY:
                    // 验证提示信息
                    String validBankInfo = collectionDTO.validBankInfo();
                    if (validBankInfo == null) {
                        list.add(collectionDTO);
                    }
                    break;
                default:
                    break;
            }
        }
        return list;
    }

    @Override
    public String correlatedFlag(Long collectionId) {
        //查询系统使用费
        int sysCount = baseMapper.getSysCount(collectionId);
        if (sysCount > 0){
            return "系统使用费已关联收款方不能被删除";
        }
        //查询标书费
        int freeCount = baseMapper.getFreeCount(collectionId);
        if (freeCount > 0){
            return "招标文件已关联收款方式不能够被删除";
        }
        //TODO 报名费不收取
        return null;
    }
}
